package com.easy_helth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy_helth.common.R;
import com.easy_helth.dto.DishDto;
import com.easy_helth.entity.Category;
import com.easy_helth.entity.Dish;
import com.easy_helth.entity.Setmeal;
import com.easy_helth.entity.User;
import com.easy_helth.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    DishFlavorService dishFlavorService;
    @Autowired
    DishService dishService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    UserService userService;
    @Autowired
    SetmealService setmealService;

    /**
     * 新增菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        dishService.saveWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){

        //构造分页构造器对象
        Page<Dish> pageInfo = new Page<>(page,pageSize);
        Page<DishDto> dishDtoPage = new Page<>();

        //条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(name != null,Dish::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Dish::getUpdateTime);

        //执行分页查询
        dishService.page(pageInfo,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");

        List<Dish> records = pageInfo.getRecords();

        List<DishDto> list = records.stream().map((item) -> {
            DishDto dishDto = new DishDto();

            BeanUtils.copyProperties(item,dishDto);

            int categoryId = item.getCategoryId();//分类id
            //根据id查询分类对象
            Category category = categoryService.getById(categoryId);

            if(category != null){
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }
            return dishDto;
        }).collect(Collectors.toList());

        dishDtoPage.setRecords(list);

        return R.success(dishDtoPage);
    }


    /**
     * 通过id获取菜品信息
     * @param id
     * @return
     */
    @GetMapping ("/{id}")
    public R<Dish> get(@PathVariable int id){
        Dish flavor = dishService.getById(id);
//        点击就加1
        flavor.setFocus(flavor.getFocus()+1);
        return R.success(flavor);
    }

    /**
     * 修改菜品信息
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        return R.success("菜品修改成功");
    }


    /**
     * 删除菜品
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable int id){
        dishService.removeById(id);
        return R.success("删除成功");
    }

    /**
     * 返回套餐列表
     * @param dish
     * @return
     */
    @GetMapping("/list")
    public R<List<Dish>> list(Dish dish){
        Integer id = dish.getId();
//        构造查询条件
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper();
//        queryWrapper.eq(id != 0,Dish::getId,dish.getId());
//        只查状态为1
        queryWrapper.eq(Dish::getStatus,1);
        queryWrapper.eq(Dish::getCategoryId,dish.getCategoryId());
        queryWrapper.orderByDesc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> dishList = dishService.list(queryWrapper);
        return R.success(dishList);
    }

    /**
     * 修改套餐状态
     * @param id
     * @return
     */
    @PostMapping("/status/{id}")
    public R<Dish> updateState(@PathVariable int id){
        Dish dish = dishService.getById(id);
        Integer status = dish.getStatus();
        if (status == 0) {
            dish.setStatus(1);
        }
        else
            dish.setStatus(0);
        return R.success(dish);
    }

    @GetMapping("/getList/{categotyId}")
    public R<List<Dish>> getDishListByCategoryId(@PathVariable int categotyId){
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dish::getCategoryId,categotyId);
        List<Dish> dishList = dishService.list(lambdaQueryWrapper);
        return R.success(dishList);
    }

//通过用户名字进行特定推荐
    @GetMapping("/getRecommend/{name}")
    public R<List<Setmeal>> getDishListByUserName(@PathVariable("name") String name){
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUserName,name);
        User user = userService.getOne(lambdaQueryWrapper);
        double height = user.getHeight()/100;
        double weight = user.getWeight();
        double BMI =  weight/height/height;
        if (BMI >= 22) {
            LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
            setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, 11);
            List<Setmeal> setmealList = setmealService.list(setmealLambdaQueryWrapper);
            return R.success(setmealList);
        }else if (BMI <= 18.5){
            LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
            setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, 12);
            List<Setmeal> setmealList = setmealService.list(setmealLambdaQueryWrapper);
            return R.success(setmealList);
        }
        return null;
    }
}
